var repayDetailData,repayTotal=0,api=frameElement.api,W=api.opener,_param_id = api.data['id'],repaymentId = api.data['repaymentId'],loanId = api.data['loanId'],repayRealDate = api.data['repayRealDate'],repayType = api.data['repayType'],deadlineNumber = api.data['deadlineNumber']; // 通过数据共享的方法获取调用页面文本框中的值;
var showPlanCapital=0,showPlanTotal=0,showPlanInterest=0,showPlanHandingFee=0,showPlanInspectFee=0,showPlanManageFee=0,showPlanOverdueFee=0,showPlanBreachFee=0,showPlanCollectionFee=0,showPlanLegalFee=0,showRepayTotal=0;
$(document).ready(function() {
	var array = new Array;
	array.push({name:'beanProxy[enable]',value:0});
	array.push({name:'beanProxy[loanId]',value:loanId});
	array.push({name:'beanProxy[repaymentId]',value:repaymentId});
	//array.push({name:'beanProxy[id_NotEqualTo]',value:_param_id});
	array.push({name:'isNonPage',value:1});
	//array.push({name:'beanProxy[deadlineNumber_NotEqualTo]',value:'0'});
	if(repayType!=1){
		//重新加载过期数据
		//array.push({name:'beanProxy[repayStatus_NotEqualTo]',value:'5'});
		var isCanRepayFuc = function(tdata, display, row){
			 var repayStatus = row.repayStatus,deadlineNumberN = row.deadlineNumber,planTotal = row.planTotal,planCapital=row.planCapital,planInterest=row.planInterest,
			     planHandingFee = row.planHandingFee,planInspectFee=row.planInspectFee,planManageFee=row.planManageFee,planOverdueFee=row.planOverdueFee,planBreachFee=row.planBreachFee,
			     planCollectionFee = row.planCollectionFee,planLegalFee=row.planLegalFee,repayTotal=row.repayTotal;
			 if('1' == repayStatus || '5' == repayStatus || '0' == repayStatus){
				 return '否';
			 }
			 if(deadlineNumberN == deadlineNumber){
				 return '否';
			 }
			 showPlanCapital = showPlanCapital + planCapital;
			 showPlanInterest = showPlanInterest + planInterest;
			 showPlanHandingFee = showPlanHandingFee + planHandingFee;
			 showPlanInspectFee = showPlanInspectFee + planInspectFee;
			 showPlanManageFee = showPlanManageFee + planManageFee;
			 showPlanOverdueFee = showPlanOverdueFee + planOverdueFee;
			 showPlanBreachFee = showPlanBreachFee + planBreachFee;
			 showPlanCollectionFee = showPlanCollectionFee + planCollectionFee;
			 showPlanLegalFee = showPlanLegalFee + planLegalFee;
			 showRepayTotal = showRepayTotal + repayTotal;
			 showPlanTotal = showPlanTotal+ planTotal ;
			 return '是';
		},planTotalFuc = function (tdata, display, row) {
			return tdata-row.planCapital;
		},planCapitalFuc = function (tdata, display, row) {
			return 0;
		};
		initLastEnterprise(array,planTotalFuc,planCapitalFuc,isCanRepayFuc);
	}else{
		//全额结算 需要重新计算lastEnterprise 把已经过期不还的金额算出来
		//array.push({name:'beanProxy[repayStatus_In]',value:'3,4'});
		//array.push({name:'beanProxy[deadlineNumber_LessThan]',value:repayDetailData.deadlineNumber});
		var isCanRepayFuc = function(tdata, display, row){
			 var repayStatus = row.repayStatus,deadlineNumberN = row.deadlineNumber,planTotal = row.planTotal,planCapital=row.planCapital,planInterest=row.planInterest,
		     planHandingFee = row.planHandingFee,planInspectFee=row.planInspectFee,planManageFee=row.planManageFee,planOverdueFee=row.planOverdueFee,planBreachFee=row.planBreachFee,
		     planCollectionFee = row.planCollectionFee,planLegalFee=row.planLegalFee,repayTotal=row.repayTotal;
			 if(deadlineNumber<=deadlineNumberN){
				 return '否';
			 }
			 if('1' == repayStatus || '5' == repayStatus || '0' == repayStatus){
				 return '否';
			 }
			 showPlanCapital = showPlanCapital + 0;
			 showPlanInterest = showPlanInterest + planInterest;
			 showPlanHandingFee = showPlanHandingFee + planHandingFee;
			 showPlanInspectFee = showPlanInspectFee + planInspectFee;
			 showPlanManageFee = showPlanManageFee + planManageFee;
			 showPlanOverdueFee = showPlanOverdueFee + planOverdueFee;
			 showPlanBreachFee = showPlanBreachFee + planBreachFee;
			 showPlanCollectionFee = showPlanCollectionFee + planCollectionFee;
			 showPlanLegalFee = showPlanLegalFee + planLegalFee;
			 showRepayTotal = showRepayTotal + repayTotal;
			 showPlanTotal = showPlanTotal+(planTotal - planCapital);
			 return '是';
		}
		initLastEnterprise(array,null,null,isCanRepayFuc);
	}
	
});

var initOther = function(){
	//查询详情
	var aoData = {repayRealDate:repayRealDate,repayType:repayType,id:_param_id,repaymentId:repaymentId};
	$.ajax( {
		type: "POST", 
		url: 'sys/LoanExt/computerRepaymentDetailThis', 
		cache : false,
		async:false,
		dataType : "json",
		data : aoData, 
		success: function(resp) {
			if(resp && resp.state == 200){
				var data = resp.data;
				if(repayType!=1){
					//重新计算应还总额
					initHtmlRepaymentDetailSpan(data,'currentRepayDetailSum');
				}else{
					//全额结算 需要重新计算lastEnterprise 把已经过期不还的金额算出来
					initHtmlRepaymentDetailSpan(data,'currentRepayDetailSum');
				}
				return false;
			}
		}
	});

};


var initHtmlRepaymentDetailSpan = function(data,sign){
	_set_lable_text('#currentRepayDetail span ','name','',false,data,function(value,name){
		var theOne = $('#'+sign+' span[name="'+name+'"]'),expired=$('#expired'+name);
		if(typeof(theOne)!='undefined' && typeof(expired)!='undefined'){
			var expiredValue = expired.text();
			if(_is_empty(expiredValue)){
				expiredValue = '0';
			}
			var sum = parseInt(value,10) + parseInt(expiredValue,10);
			theOne.text(sum);
		}
	});
};


var initLastEnterprise  = function(array,planTotal,planCapital,isCanRepay){
	if(typeof(planTotal)=='undefined'){
		planTotal=	function (data, display, row) {
			return data;
		};
	};
	if(typeof(planCapital)=='undefined'){
		planCapital=	function (data, display, row) {
			return data;
		};
	};
	
	$('#lastEnterprise').dataTable({
		"bProcessing" : true, //DataTables载入数据时，是否显示‘进度’提示
		"bServerSide" : true, //是否启动服务器端数据导入
		"bStateSave" : false, //是否打开客户端状态记录功能,此功能在ajax刷新纪录的时候不会将个性化设定回复为初始化状态
		"bJQueryUI" : true, //是否使用 jQury的UI theme
		//"aLengthMenu" : [5, 10, 15], //更改显示记录数选项
		"iDisplayLength" : 10, //默认显示的记录数
		"bAutoWidth" : true, //是否自适应宽度 update
		"bScrollCollapse" : false, //是否开启DataTables的高度自适应，当数据条数不够分页数据条数的时候，插件高度是否随数据条数而改变
		"bPaginate" : false, //是否显示（应用）分页器
		"bInfo" : false, //是否显示页脚信息，DataTables插件左下角显示记录数
		"sPaginationType" : "full_numbers", //详细分页组，可以支持直接跳转到某页
		"bSort" : true, //是否启动各个字段的排序功能
		"aaSorting" : [[0, "ASC"]], //默认的排序方式，第3列，升序排列
		"bFilter" : false, //是否启动过滤、搜索功能
		"sPaginationType": "full_numbers",
		"sScrollX": '100%',
		'destroy':true,
		"sScrollXInner": 1350,
		"sAjaxSource":"sys/LoanRepaymentDetail/list",
		"fnServerData": function( sSource, aoData, fnCallback ) {
			$.merge(aoData,array);//合并
			$.unique(aoData);//去重
			$.ajax( {
				type: "POST", 
				url: sSource, 
				cache : false,
				dataType : "json",
				data : aoData, 
				success: function(resp) {
					if(resp && resp.state == 200){
						fnCallback(resp.datagrid);
						return false;
					}
					fnCallback(null);
				}
			});
		},  //自定义数据获取函数
		"aoColumns" : [ {
			"mDataProp" : "deadlineNumber",
			"sTitle" : "期数",
			"sDefaultContent" : "",
			"sClass" : "center",
			"bSortable": false
		},{
			"mDataProp" : "planCapital",
			"sTitle" : "本金",
			"sDefaultContent" : "",
			"sClass" : "center",
			"bSortable": false,
			"mRender":planCapital
		},{
			"mDataProp" : "planInterest",
			"sTitle" : "利息",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		}, {
			"mDataProp" : "planHandingFee",
			"sTitle" : "手续费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planInspectFee",
			"sTitle" : "调查费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planManageFee",
			"sTitle" : "管理费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planOverdueFee",
			"sTitle" : "滞纳金",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planBreachFee",
			"sTitle" : "违约金",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planCollectionFee",
			"sTitle" : "催收费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planLegalFee",
			"sTitle" : "法诉费",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "planTotal",
			"sTitle" : "总计",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender":planTotal
		},{
			"mDataProp" : "repayTotal",
			"sTitle" : "收款金额",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center"
		},{
			"mDataProp" : "uid",
			"sTitle" : "是否需要加入还款",
			"bSortable": false,
			"sDefaultContent" : "",
			"sClass" : "center",
			"mRender":isCanRepay
		}
		],
		"oLanguage": {
			"sUrl":top.basePath+"static_resources/language/lang-cn.txt" //这个是国际化，也就是汉化的各种文字数据源，写对url就行了
		},
		"footerCallback": function (tfoot, row, data, start, end, display ) {
			var api = this.api(), data;
			var intVal = function (i) {
				return typeof i === 'string' ?
						i.replace(/[\$,]/g, '')*1 :
							typeof i === 'number' ?
									i : 0;
			};
			
			$('#expiredplanCapital').text(showPlanCapital);
			$('#expiredplanInterest').text(showPlanInterest);
			$('#expiredplanHandingFee').text(showPlanHandingFee);
			$('#expiredplanInspectFee').text(showPlanInspectFee);
			$('#expiredplanManageFee').text(showPlanManageFee);
			$('#expiredplanOverdueFee').text(showPlanOverdueFee);
			$('#expiredplanBreachFee').text(showPlanBreachFee);
			$('#expiredplanCollectionFee').text(showPlanCollectionFee);
			$('#expiredplanLegalFee').text(showPlanLegalFee);
			$('#expiredplanTotal').text(showPlanTotal);
			$('#expiredrepayTotal').text(showRepayTotal);
			
//			pageTotal1 = api.column(1, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			if(repayType!=1){
//				$('#expiredplanCapital').text(0);
//				$(tfoot).find('th').eq(1).html(0);
//			}else{
//				$('#expiredplanCapital').text(pageTotal1);
//				$(tfoot).find('th').eq(1).html(pageTotal1);
//			}
//			
//
//			pageTotal2 = api.column(2, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(2).html(pageTotal2 );
//			$('#expiredplanInterest').text(pageTotal2);
//
//			pageTotal3 = api.column(3, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(3).html(pageTotal3 );
//			$('#expiredplanHandingFee').text(pageTotal3);
//
//			pageTotal4 = api.column(4, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(4).html(pageTotal4);
//			$('#expiredplanInspectFee').text(pageTotal4);
//
//			pageTotal5 = api.column(5, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(5).html(pageTotal5 );
//			$('#expiredplanManageFee').text(pageTotal5);
//
//			pageTotal6 = api.column(6, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(6).html(pageTotal6);
//			$('#expiredplanOverdueFee').text(pageTotal6);
//
//			pageTotal7 = api.column(7, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(7).html(pageTotal7);
//			$('#expiredplanBreachFee').text(pageTotal7);
//
//			pageTotal8 = api.column(8, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(8).html(pageTotal8);
//			$('#expiredplanCollectionFee').text(pageTotal8);
//
//			pageTotal9 = api.column(9, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			$(tfoot).find('th').eq(9).html(pageTotal9);
//			$('#expiredplanLegalFee').text(pageTotal9);
//
//			pageTotal10 = api.column(10, { page: 'current'}).data().reduce( function (a, b) {
//				return intVal(a) + intVal(b);
//			}, 0);
//			if(repayType!=1){
//				pageTotal10 = pageTotal10 - pageTotal1;
//			}
//			$(tfoot).find('th').eq(10).html(pageTotal10);
//			$('#expiredplanTotal').text(pageTotal10);
			
			//初始化其他
			initOther();

		}
	});

};
